package com.haixue.android.academy;

import android.academy.haixue.com.haixue.R;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.haixue.android.academy.activity.BaseLoginActivity;
import com.haixue.android.academy.activity.NavigationActivity;
import com.haixue.android.academy.utils.IntentUtils;

import cn.woblog.android.common.utils.TimeUtils;


public class MainActivity extends BaseLoginActivity {
    private static final int NOT_AUTO_LOGIN = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        log.debug("onCreate");
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        String[] userInfo = sp.getUserInfo();
        if (userInfo!=null&& userInfo[1]!=null){
            log.debug("auto login");
            super.doLogin(userInfo[0],userInfo[1]);
        }else{
            TimeUtils.delayed(3000,this,NOT_AUTO_LOGIN);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDelayedSuccess(int flag) {
        switch (flag){
            case NOT_AUTO_LOGIN:
                if (sp.isShowNavigation()){
                    log.debug("to show navigation");
                    IntentUtils.toActivityAfterFinish(getContext(), NavigationActivity.class);
                }else {
                    log.debug("to login activity");
                    super.toLoginActivity();
                }
                break;
            case TO_HOME_ACTIVITY:
                toHomeActivity();
                break;
        }
    }


}
